
import router from '@/router'

const Layout = () => import('@/layout')
const getRoutePath = {
  // 权限
  'menu': '/permission/menu/index',
  'icon': '/permission/icon/index',
  'role': '/permission/roles/index',
  'user': '/permission/user/index',
  // 日志
  'back': '/log/admin-log/index',
  // 图片管理
  'award_image': '/image/award-image/index',
  'img_product_detial': '/image/device-detail/index',
  'img_product_present': '/image/device-present/index',
  'img_product': '/image/device-image/index'
}

export default (roles) => {
  roles.forEach(v => {
    v.meta = {
      title: v.name,
      icon: v.iconUrl,
      ...v
    }
    v.name = v.componentName
    v.children = routerChildren(v.children)
    if (v.componentPath === '@/layout') {
      v.component = Layout
    } else {
      v.component = routerCom(v.componentName)
    }
  })

  router.addRoutes(roles)

  return roles
}

function routerCom(componentName) { // 对路由的component解析
  return (resolve) => require([`@/views${getRoutePath[componentName]}.vue`], resolve)
}

function routerChildren(children) { // 对子路由的component解析
  children.forEach(v => {
    v.meta = {
      title: v.name,
      icon: v.iconUrl,
      ...v
    }
    v.name = v.componentName

    if (v.componentPath === '@/layout') {
      v.component = Layout
    } else {
      v.component = routerCom(v.componentName)
    }
    if (v.children != undefined) {
      v.children = routerChildren(v.children)
    }
  })
  return children
}
